<template>
    <div>
        <Header></Header>

        <el-container>

            <el-aside>
                <div style="margin-top: 100px">
                    <el-button type="primary" @click="handleAddTop">添加顶级群组</el-button>
                    <el-tree :data="treedata" default-expand-all
                             :props="defaultProps"
                             :expand-on-click-node="false" style="font-size: 18px;line-height: 30px">
                        <span class="custom-tree-node" slot-scope="{ node, data }">
                        <span @click="shownode(data)">{{ data.name }}</span>
                           <span>
                            <i class="el-icon-circle-plus-outline" @click="addNode(data)"></i>
                               <i class="el-icon-remove-outline" @click="delNode(node, data)"></i>
                           </span>
                        </span>
                    </el-tree>

                </div>
            </el-aside>


            <el-dialog title="新增群组"
                       :visible.sync="showdialogroot"

                       :before-close="handleClose">
                <el-row style="width: 40%;margin-left: 30%">
                    <el-form>

                        <el-form-item label="群组名称" label-width="120px">
                            <el-input v-model="title"></el-input>
                        </el-form-item>
                    </el-form>
                </el-row>

                <span slot="footer" class="dialog-footer">
                        <el-button @click="showdialogroot = false">{{$t('standard.cancel')}}</el-button>
                        <el-button type="primary" @click="addGroup()">{{$t('standard.confirm')}}</el-button>
                    </span>

            </el-dialog>


            <el-main v-show="showmain">
                <el-row style="text-align: left;height: 50px;" >
                    <div style="font-size: 36px;height: 50px;">{{groupname}}</div>
                </el-row>
                <el-tabs v-model="activeName" @tab-click="tabClick">
                    <el-tab-pane label="设备管理" name="device">
                    </el-tab-pane>
                    <el-tab-pane label="人脸管理" name="person">
                        <persons ref="persons"></persons>
                    </el-tab-pane>
                    <el-tab-pane label="图像管理" name="media">
                        <mediaVue ref="media"></mediaVue>
                    </el-tab-pane>
                </el-tabs>
            </el-main>
        </el-container>
    </div>

</template>

<script>
    import VmImageList from '../vm-image-list.vue'
    import Header from '../Header.vue'
    import apitree from '@/resource/api'
    import api from '../../axios';
    import persons from "../../pages/persons";
    import mediaVue from "../../pages/media";


    let id = 1000;

    export default {
        name: 'ImageList',
        components: {
            VmImageList,
            Header,
            persons,
            mediaVue
        },
        data() {
            return {
                activeName: 'person',
                data4: 'hello',
                name: 'shengwm',
                treedata: [
                    {
                        name: '群组4',
                        id: '15',
                        pid: '',
                        isEdit: false,
                        children: []
                    }
                ],
                showdialog: false,
                dataImageList: '',
                face: {
                    name: '',
                    group: '',
                    file: ''
                },
                defaultProps: {
                    children: 'children',
                    label: 'text',
                    id: 'id'
                },
                fileList: [],
                groupname: '',
                showdialogroot: false,
                title: '',
                curpid: 0,
                showmain: false,
                groupid: 0
            }
        },


        mounted() {
            this.listGroup();

        },
        methods: {
            tabClick(tab) {
                if (tab.name === 'media') {
                    this.$refs.media.getCompanyId();
                    this.$refs.media.query(1);
                } else if (tab.name === 'person') {
                    this.$refs.persons.getCompanyId();
                    this.$refs.persons.query(1);
                }
            },
            deletefn: function (data) {
                for (let i = 0; i < this.dataImageList.length; i++) {
                    if (this.dataImageList[i].id === data.id) {
                        this.dataImageList.splice(i, 1)
                    }
                }
            },
            getFile(event) {

                console.log(event.target.files[0])
            },
            addNode(data) {
                console.log(data)
                this.curpid = data.id;
                this.showdialogroot = true;

            },

            delNode(node, data) {
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                children.splice(index, 1);
            },
            addFace() {
                let formdata = new FormData();
                formdata.append("name", this.face.name);
                formdata.append("groupId", this.groupid);
                var file = document.getElementById("file").files[0];
                formdata.append("file", file);


                console.log(file);
                api.addFace(formdata).then(({data}) => {
                    if (data.data) {
                        if (data.success == true) {
                            this.showdialog = false;
                        }

                    }
                });

            },
            handleClick() {
                console.log(this.showdialog);
                this.showdialog = true;

            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            shownode(node) {
                this.groupname = node.name;
                this.showmain = true;
                this.groupid = node.id;
                localStorage.setItem("companyId", node.id);
                this.$refs.persons.getCompanyId();
                this.$refs.persons.query(1);
                //this.listFace(node.id);
            },
            handleAddTop() {
                /*  this.treedata.push({
                      name: '新增节点',
                      id: '100',
                      pid: '',
                      isEdit: false,
                      children: []
                  })*/
                this.showdialogroot = true;
            },
            addGroup() {
                if (this.title != '') {
                    api.addGroup(this.title, this.curpid).then(({data}) => {
                        if (data.data) {
                            console.log(data)
                            this.content = data.data;
                            if (data.success == true) {
                                this.showdialogroot = false;
                            }

                        }
                    });
                }
                alert(this.title);
            },
            listGroup() {
                var user = JSON.parse(localStorage.getItem("userInfo"));
                var username = user.username;
                api.listGroup(username).then(({data}) => {
                    if (data.data) {
                        this.treedata = data.data;

                    }
                });
            },
            listFace(groupid) {
                api.listFace(groupid).then(({data}) => {
                    if (data.data) {
                        var json = eval(data.data);
                        this.dataImageList = json;
                        json.forEach((item) => {
                            console.log(item);
                        });
                    }
                });
            }
        }
    }
</script>


<style>
    .el-header, .el-footer {
        background-color: #B3C0D1;
        color: #333;
        text-align: center;
        line-height: 60px;
    }

    .el-aside {
        margin-top: 60px;
        background-color: white;
        color: #333;
        text-align: center;
        line-height: 200px;
    }

    .el-main {
        margin-top: 60px;
        background-color: #ffffff;
        color: #333;
        text-align: center;
        /*line-height: 160px;*/
    }

    body > .el-container {
        margin-bottom: 40px;
        margin-top: 60px;
    }

    /*.el-container:nth-child(5) .el-aside,*/
    /*.el-container:nth-child(6) .el-aside {*/
        /*line-height: 260px;*/
    /*}*/

    /*.el-container:nth-child(7) .el-aside {*/
        /*line-height: 320px;*/
    /*}*/
</style>

